package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.ChinaCityAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.ListToStringUtil;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textillexepres.db.AddreaCitybeans;
import com.itboye.textillexepres.db.DBManager;

/**
 * 中国省份列表
 * 
 * @author sy
 * 
 */
public class ActivityAddreeStart extends BaseActivity implements
		OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private DBManager dbManager;
	public String city;
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	String codeId = "1";
	TextView title;
	String type;
	private ImageView img_other;
	String country;
	// listview头部
	CityBeans cityModel;
	private ListToStringUtil stringUtil;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);

		mCityNames = getCityNames();
		cityModel = new CityBeans();
		handler = new Handler();
		type=getIntent().getStringExtra("type");
		country=getIntent().getStringExtra("title");
		setAdapter(mCityNames);
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
		title.setText("我的地址");
		img_other.setVisibility(View.GONE);
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_province where countryid=" + codeId
						+ " ORDER BY province", null);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setProvince(cursor.getString(cursor
					.getColumnIndex("province")));

			cityModel.setId(cursor.getString(cursor
					.getColumnIndex("provinceID")));

			names.add(cityModel);
		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			// if (pos == 0) {
			// return;
			// }
			Intent intent2 = new Intent("CHIONA");
			sendBroadcast(intent2);

			searchType = true;
			cityModel = (CityBeans) country_lv_list.getAdapter().getItem(
					pos);
			city = cityModel.getProvince();
			cityModel.getId();

			String CityId = cityModel.getId();
			getPrivateNames(CityId);
			finish();
			
		

		}
	}

	@SuppressWarnings({ "unused", "static-access" })
	private String getPrivateNames(String codeId) {
		ArrayList<AddreaCitybeans> names = new ArrayList<AddreaCitybeans>();

		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_city where father=" + codeId
						+ " ORDER BY city", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			AddreaCitybeans cityModel = new AddreaCitybeans();
			cityModel.setProvince(cursor.getString(cursor
					.getColumnIndex("city")));

			cityModel.setId(cursor.getString(cursor.getColumnIndex("cityID")));
			names.add(cityModel);

		}
		String CityId = cityModel.getId();
		if (CityId.equals("310000") || CityId.equals("110000")
				|| CityId.equals("120000") || CityId.equals("500000")) {
			Intent intent = new Intent(ActivityAddreeStart.this,
					ActivityAreaChina.class);
			intent.putExtra("id", cityModel.getId());
			intent.putExtra("city", country+cityModel.getProvince());
			intent.putExtra("type", type);
			intent.putExtra("provinceID",cityModel.getId());
			intent.putExtra("countryNmae",city);
			startActivity(intent);
			System.out.println("provinceID"+cityModel.getId());
		} else {
			System.out.println("shanghai" + cityModel.getId());
			Intent intent = new Intent(ActivityAddreeStart.this,
					ActivityChinaCity.class);
			intent.putExtra("chinaID", cityModel.getId());
			intent.putExtra("type",type);
			intent.putExtra("province",country+ cityModel.getProvince());
			intent.putExtra("countryNmae",country);
			startActivity(intent);
			
			System.out.println("china Is CityId and area" + country+cityModel.getProvince());
		}
		return stringUtil.ListToString(names);

	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new ChinaCityAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}